package pro.cherish.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-6-15 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：HttpHelloWorldServerInitializer
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-6-15 
 */
public class HttpHelloWorldServerInitializer extends ChannelInitializer<SocketChannel> {
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		EventExecutorGroup executors = new DefaultEventExecutorGroup(1);
		ChannelPipeline pipeline = ch.pipeline();
		/**
		 * 或者使用HttpRequestDecoder & HttpResponseEncoder
		 */
		pipeline.addLast(new HttpServerCodec());
		// 用于上传文件
		ch.pipeline().addLast(executors, new HttUploadHandler());
		pipeline.addLast(new HttpObjectAggregator(1024*124));
		// 新增ChunkedHandler，主要作用是支持异步发送大的码流（例如大文件传输），但是不占用过多的内存，防止发生java内存溢出错误
		ch.pipeline().addLast(new ChunkedWriteHandler());
		// 用于下载文件
		ch.pipeline().addLast(new HttpDownloadHandler());
		pipeline.addLast(new HttpServerExpectContinueHandler());
		pipeline.addLast(new HttpHelloWorldServerHandler());

	}
}
